/*
   Zii EGGxplorer - A simple Zii file browser

   Copyright (C) 2009 - 2010 Claudio Corti

   Implementation file for Image class, derived from CImage; it allows copying one CImage on top of
   another creating a new one or modifying an existent one.
*/

// Application headers
#include "Image.h"

// Constructor
Image::Image() {}

// Destructor
Image::~Image() {}

// Copy first Image on top of the second Image, using given CRects, creating a new Image
Image *Image::Copy(CSequencer *Sequencer, Image *FirstImage, Image *SecondImage,
                          CRect &FirstRect, CRect &SecondRect)
{
   CBlitLib *blit = CBlitLib::Create(Sequencer);

   int width = SecondImage->GetWidth();
   int height = SecondImage->GetHeight();

   // Create the new image
   Image *image = new Image();
   image->Create(width, height);

   // Copy SecondImage CBuffer into image CBuffer
   blit->SetCopyDimensionsSelect(CBlitLib::COPYDIMENSIONS_SOURCE);
   blit->EnableState(CBlitLib::TRANSPARENCY);
   blit->Copy(SecondImage->mpBuffer, 0, 0, image->mpBuffer, 0, 0, width, height);

   // Enable transparency and copy FirstImage CBuffer into image CBuffer, using given CRects
   blit->Copy(FirstImage->mpBuffer, FirstRect.X, FirstRect.Y, image->mpBuffer, SecondRect.X,
              SecondRect.Y, FirstRect.W, FirstRect.H);
   blit->WaitForCompletion();
   blit->DisableState(CBlitLib::TRANSPARENCY);

   delete blit;

   return image;
}

// Copy first Image on top of the second Image, using given CRects, modifying the Image
// specified by ImageCopyDestination
void Image::Copy(CSequencer *Sequencer, Image *FirstImage, Image *SecondImage,
                          CRect &FirstRect, CRect &SecondRect,
                          ImageCopyDestination DestinationImage)
{
   CBlitLib *blit = CBlitLib::Create(Sequencer);

   Image *image_dst, *image_src;
   CRect source, dest;

   if (DestinationImage == FIRST)
   {
      image_dst = FirstImage;
      dest = FirstRect;
      image_src = SecondImage;
      source = SecondRect;
   }
   else
   {
      image_src = FirstImage;
      source = FirstRect;
      image_dst = SecondImage;
      dest = SecondRect;
   }

   blit->EnableState(CBlitLib::TRANSPARENCY);
   blit->SetCopyDimensionsSelect(CBlitLib::COPYDIMENSIONS_SOURCE);
   image_src->Blit(blit, source.X, source.Y, source.W, source.H, image_dst->mpBuffer, dest.X,
      dest.Y);
   blit->DisableState(CBlitLib::TRANSPARENCY);

   delete blit;
}
